package cn.edu.nju.software.model.administrator;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.nju.software.model.DatabaseHelper;

public class DisplayManager {

	private static DatabaseHelper dbHelper;

	public DisplayManager() {
		if (dbHelper == null) {
			dbHelper = new DatabaseHelper();
		}
	}
	
	public ResultSet fetchAllDisplays() {
		ResultSet rs = dbHelper.fetchAllRecord(DatabaseHelper.TID_DISPLAY);
		return rs;
	}
	
	public String[] fetchAllHalls() {
		ResultSet rs = dbHelper.fetchAllHalls();
		try {
			if (rs != null) {
				rs.last();
				int count = rs.getRow();
				rs.beforeFirst();

				String[] halls = new String[count];
				for (int i = 0; rs.next(); i++) {
					halls[i] = rs.getString(1);
				}
				return halls;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String[] fetchAllMovies() {
		ResultSet rs = dbHelper.fetchAllMovies();
		try {
			if (rs != null) {
				rs.last();
				int count = rs.getRow();
				rs.beforeFirst();
				
				String[] movies = new String[count];
				for (int i = 0; rs.next(); i++) {
					movies[i] = rs.getString(1);
				}
				return movies;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean addDisplay(String hall, String movie, String time, int capacity,
			int status, int surplus, int price) {
		boolean result = false;

		result = true;
		dbHelper.addDisplay(hall, movie, time, capacity, status, surplus, price);

		return result;
	}
	
	public boolean alterDisplay(String hall, String movie, String time, int capacity,
			int status, int surplus, int price) {
		boolean result = false;
		
		result = true;
		dbHelper.alterDisplay(hall, movie, time, capacity, status, surplus, price);
		
		return result;
	}
}
